/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <string>
#include <vector>
#include <map>
#include <optional>
#include <nlohmann/json.hpp>
#include <functional>
#include <future>
#include "utils_noncopyable.h"
#include <memory>

namespace El {
namespace Protocol {

class IProtocol;
using IProtocolPtr = std::shared_ptr<IProtocol>;

class IProtocol {
public:
    virtual ~IProtocol() = default;

    // 创建协议实例
    static IProtocolPtr GetCustomInstance(std::string instanceName);

    // 解析请求
    virtual bool ParseRequest(const std::string &message,
                              std::string &id,
                              std::string &method,
                              nlohmann::json &params) = 0;

    // 解析应答
    virtual bool ParseResponse(const std::string &message, std::string &id, nlohmann::json &result) = 0;

    // 处理协议消息
    virtual bool HandleMessage(std::string &id,
                               std::string &method,
                               nlohmann::json &params,
                               nlohmann::json &response) = 0;

    // 创建请求
    virtual std::string CreateRequest(const std::string &id,
                                      const std::string &method,
                                      const nlohmann::json &params = nullptr) = 0;

    // 创建错误响应
    virtual std::string CreateErrorResponse(const std::string &id,
                                               int code,
                                               const std::string &message,
                                               const nlohmann::json &data = nullptr) = 0;
};

} // namespace Protocol
} // namespace El
